package com.ktgj.service.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.constant.FileConstants;
import com.ktgj.common.core.utils.StringUtils;
import com.ktgj.service.domain.VipImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.service.domain.ViphallAttachment;
import com.ktgj.service.service.IViphallAttachmentService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 贵宾厅附件（厅图片）Controller
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@ControllerAdvice
@RestController
@RequestMapping("/attachment")
public class ViphallAttachmentController extends BaseController
{
    @Autowired
    private IViphallAttachmentService viphallAttachmentService;

    /**
     * 查询贵宾厅附件（厅图片）列表
     */
    //@PreAuthorize(hasPermi = "service:attachment:list")
    @GetMapping("/list")
    public TableDataInfo list(ViphallAttachment viphallAttachment)
    {
        startPage();
        List<ViphallAttachment> list = viphallAttachmentService.selectViphallAttachmentList(viphallAttachment);
        return getDataTable(list);
    }

    /**
     * 导出贵宾厅附件（厅图片）列表
     */
    //@PreAuthorize(hasPermi = "service:attachment:export")
    @Log(title = "贵宾厅附件（厅图片）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ViphallAttachment viphallAttachment) throws IOException
    {
        List<ViphallAttachment> list = viphallAttachmentService.selectViphallAttachmentList(viphallAttachment);
        ExcelUtil<ViphallAttachment> util = new ExcelUtil<ViphallAttachment>(ViphallAttachment.class);
        util.exportExcel(response, list, "贵宾厅附件（厅图片）数据");
    }

    /**
     * 获取贵宾厅附件（厅图片）详细信息
     */
    //@PreAuthorize(hasPermi = "service:attachment:query")
    @GetMapping(value = "/{linkId}")
    public AjaxResult getInfo(@PathVariable("linkId") Long linkId)
    {
        return AjaxResult.success(viphallAttachmentService.selectViphallAttachmentById(linkId));
    }

    /**
     * 新增贵宾厅附件（厅图片）
     */
    //@PreAuthorize(hasPermi = "service:attachment:add")
    @Log(title = "贵宾厅附件（厅图片）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ViphallAttachment viphallAttachment)
    {
        return toAjax(viphallAttachmentService.insertViphallAttachment(viphallAttachment));
    }

    /**
     * 修改贵宾厅附件（厅图片）
     */
    //@PreAuthorize(hasPermi = "service:attachment:edit")
    @Log(title = "贵宾厅附件（厅图片）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ViphallAttachment viphallAttachment)
    {
        return toAjax(viphallAttachmentService.updateViphallAttachment(viphallAttachment));
    }

    /**
     * 删除贵宾厅附件（厅图片）
     */
    //@PreAuthorize(hasPermi = "service:attachment:remove")
    @Log(title = "贵宾厅附件（厅图片）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{linkIds}")
    public AjaxResult remove(@PathVariable Long[] linkIds)
    {
        return toAjax(viphallAttachmentService.deleteViphallAttachmentByIds(linkIds));
    }

    /**
     * 贵宾厅图文上传
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/importImg")
    public AjaxResult importImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        boolean isFlag = false;
        for (String type : FileConstants.IMAGE_TYPE) {
            if (StringUtils.endsWithIgnoreCase(file.getOriginalFilename(), type)) {
                isFlag = true;
                break;
            }
        }
        if (isFlag) {
            VipImage vipImage = viphallAttachmentService.importImg(file, request);
            boolean isLegal = vipImage.isLegal();
            if (isLegal) {
                Map resultMap = new HashMap();
                resultMap.put("imgPath", vipImage.getIsPath());
                return AjaxResult.success(resultMap);
            } else {
                return AjaxResult.error("图片上传错误");
            }
        } else {
            return AjaxResult.error("图片格式错误");
        }
    }

    @PostMapping("/importImgs")
    public AjaxResult importImgs(MultipartFile[] files, HttpServletRequest request) {
        boolean isFlag = false;
        for (MultipartFile multipartFile : files) {
            for (String type : FileConstants.IMAGE_TYPE) {
                if (StringUtils.endsWithIgnoreCase(multipartFile.getOriginalFilename(), type)) {
                    isFlag = true;
                    break;
                }
            }
        }
        if (isFlag) {
            VipImage vipImage = viphallAttachmentService.importImgs(files, request);
            boolean isLegal = vipImage.isLegal();
            if (isLegal) {
                Map resultMap = new HashMap();
                resultMap.put("imgPaths", vipImage.getIsPaths());
                return AjaxResult.success(resultMap);
            } else {
                return AjaxResult.error("图片上传错误");
            }
        } else {
            return AjaxResult.error("图片格式错误");
        }
    }
}
